Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Array → Array Methods

Python Array

Array Methods

Array methods in python

While Python doesn't have a built-in "array" type in the same way as languages like C or Java, it offers several ways to work with ordered collections of data that effectively function as arrays. The most common choices are lists and NumPy arrays. Let's explore them, focusing on their methods:

Python Arrays

Lists(Arrays) are versatile, mutable (changeable) sequences that can hold elements of different data types. They are the closest equivalent to arrays in Python's built-in functionality.

Array Methods

Lists provide a rich set of methods for manipulating their contents. Here are some key examples: 1. `append(item)` Adds an item to the end of the list.
append(item) method example my_list = [1, 2, 3] my_list.append(4) print(my_list)

Output

[1, 2, 3, 4]

2. `insert(index, item)` Inserts an item at a specific index.
insert(index, item) method example in python my_list = [1, 2, 3] my_list.insert(1, 10) print(my_list)

Output

[1, 10, 2, 3]

3. `extend(iterable)` Adds all items from an iterable (like another list or tuple) to the end.
extend(iterable) method example in python my_list = [1, 2, 3] my_list.extend([5, 6]) print(my_list)

Output

[1, 2, 3, 5, 6]

4. `remove(item)` Removes the first occurrence of a specific item. Raises a `ValueError` if the item is not found.
remove(item) method example in python my_list = [1, 2, 3] my_list.remove(2) print(my_list)

Output

[1, 3]

5. `pop([index])` Removes and returns the item at a specific index (default is the last item).
pop([index]) method example in python my_list = [1, 2, 3] removed_item = my_list.pop(1) print(my_list) print(removed_item)

Output

[1, 3] 2

6. `index(item)` Returns the index of the first occurrence of an item. Raises a `ValueError` if the item is not found.
index(item) method example in python my_list = [1, 2, 3] index_of_2 = my_list.index(2) print(index_of_2)

Output

1

7. `count(item)` Returns the number of times an item appears in the list.
count(item) method example in python my_list = [1, 2, 3, 4, 5] index_of_2 = my_list.index(2) print(index_of_2) count_of_5 = my_list.count(5) print(count_of_5)

Output

1 1

8. `sort()` Sorts the list in ascending order (in-place). For custom sorting, use the `key` argument.
sort() method example in python numbers = [3, 1, 4, 1, 5, 9, 2, 6] numbers.sort() print(numbers) words = ["banana", "apple", "cherry"] words.sort(key=len) # sort by length of strings print(words)

Output

[1, 1, 2, 3, 4, 5, 6, 9] ['apple', 'banana', 'cherry']

9. `reverse()` Reverses the order of elements in the list (in-place).
reverse() method example in python numbers = [3, 1, 4, 1, 5, 9, 2, 6] numbers.reverse() print(numbers)

Output

[6, 2, 9, 5, 1, 4, 1, 3]

10. `copy()` Creates a shallow copy of the list.
copy() method example in python my_list=[3, 1, 4, 1, 5, 9, 2, 6] list_copy = my_list.copy() list_copy.append(7) print(my_list) # Original list remains unchanged print(list_copy)

Output

[3, 1, 4, 1, 5, 9, 2, 6] [3, 1, 4, 1, 5, 9, 2, 6, 7]

11. `clear()` Removes all items from the list.
clear() method example in python my_list=[3, 1, 4, 1, 5, 9, 2, 6] my_list.clear() print(my_list)

Output

[]

This comprehensive overview, with numerous examples, should provide a strong understanding of array-like structures and their methods in Python. Remember to consult the official Python and NumPy documentation for a complete list of available methods and their detailed specifications.

Tutorials